/*********************
//JORGE FERNANDO GOMEZ
//CMPUT 201 LEC A1
**********************/

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>

#define KW_NUM 8
#define KW_LEN 12
#define MAX_LEN 80
#define MAX_LINES 64

typedef struct {
    char aline[KW_LEN];
} SLine;

int file_error = 0;
int keywordStats[1][KW_NUM] = {0};

int readLines (FILE *, SLine *, int *);
int validateLine (char *, int, int *);
void printStats (void);
void chomp2 (char *);

int main (int argc, char * argv[]) {

    int cline = 0;
    FILE * inputFile;
    SLine Lines[MAX_LINES];

    // Print date & args
    printf("%s started on ", argv[0]);
    fflush(NULL);
    system("date");
    printf("Input file: ./%s\n", argv[1]);

    if (argc == 2) {
        inputFile = fopen(argv[argc-1], "r");

        if (inputFile == NULL) {
            printf("Can't open file \"%s\"\n", argv[argc-1]);
            exit(-1);
        }

        readLines(inputFile, Lines, &cline);

        if ( file_error ) {
            printf("Line %2d of '%s' contains invalid formatting.\n", cline, argv[argc-1]);
            printf("\t\"%s\"", Lines[cline].aline);
        }

        /* Print the command statistics from the input file */
        printStats();


    } else {
        printf("You need to enter exactly 1 input file as argument.\n");
        exit(-1);
    }


    return 0;
}

int readLines (FILE * inputFile, SLine * Lines, int * cline) {

    int nestCount = 0;
    while (*cline < MAX_LINES) {
        if ( fgets(Lines[*cline].aline, MAX_LEN, inputFile) != NULL ) {

            chomp2(Lines[*cline].aline);
            printf("Read #%2d: \"%s\"\n", *cline, Lines[*cline].aline);


            file_error = validateLine(Lines[*cline].aline, *cline, &nestCount);

            if ( file_error ) {
                return(-1);
            }


            (*cline)++;

        } else {
            (*cline)++;
            break;
        }
    }
    return 0;
}

int validateLine (char * line, int cline, int * nestCount) {
    if ( strncmp(line, "printFigure", strlen("printFigure"))) {
        /* printFigure INDEX = > 3 */
        keywordStats[*nestCount][3]++;
    }
    else if ( strncmp ( line, "drawFigure", strlen("drawFigure")) ) {
        /* drawFigure INDEX => 4 */
        keywordStats[*nestCount][4]++;
    }
    else if ( strncmp( line, "translate", strlen("translate")) ) {
        /* translate INDEX => 5 */
        keywordStats[*nestCount][5]++;
    }
    else if ( strncmp( line, "Figure", strlen("Figure")) ) {
        /* Figure INDEX => 0 */
        (*nestCount)++;
        keywordStats[*nestCount][0]++;
    }
    else if ( strncmp( line, "child", strlen("child")) ) {
        /* child INDEX => 6 */
        keywordStats[*nestCount][6]++;
    }
    else if ( strncmp( line, "draw", strlen("draw")) ) {
        /* draw INDEX => 2 */
        keywordStats[*nestCount][2]++;
    }
    else if ( strncmp( line, "end", strlen("end")) ) {
        /* end INDEX => 7 */
        (*nestCount)--;
        keywordStats[*nestCount][7]++;
    }
    else if ( strncmp( line, "#", strlen("#")) ) {
        /* # INDEX => 1 */
        keywordStats[*nestCount][1]++;
    }
    else {
       return(-1);
    }

    return 0;
}

void printStats (void) {
    int nested, index;
    char currentKeyword[KW_LEN];

    for (nested = 1, index = 0; nested == 1; index++) {
        switch ( index ) {
            case 0: //currentKeyword = "Figure";
                    printf("%d %s definition(s)\n", keywordStats[nested][index], "Figure");
                    break;
            case 2: //currentKeyword = "draw";
                    printf("%d %s command(s) within Figure\n", keywordStats[nested][index], "draw");
                    break;
            case 7: //currentKeyword = "end";
                    printf("%d %s command(s)\n", keywordStats[nested][index], "end");
                    break;
            default: break;
        }



    }

}

void chomp2 (char * line) {
    int len = strlen(line);
    int offset = 1;

    while ((line[len - offset] == '\n') || (line[len - offset] == ' '))  {
        offset++;
    }

    line[len - offset + 1] = '\0';
}
